package shopping.cart;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class Connect {

	private static String URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false";
	private static String driverName = "com.mysql.jdbc.Driver";
	private static String userName = "root";
	private static String password = "ty123456";
	
	public ArrayList<Goods> getAllgoods(){
		ArrayList<Goods> alist=new ArrayList<>();
		Connection ct = null;
		Statement st = null;
		ResultSet rs=null;
		try{
			Class.forName(driverName);
			ct = DriverManager.getConnection(URL, userName, password);
			st=ct.createStatement();
			String sql="select * from goods";
			rs=st.executeQuery(sql);
			while(rs.next()){
				alist.add(new Goods(String.valueOf(rs.getInt(1)),rs.getString("name"),rs.getDouble("price")));
			}
		}catch(SQLException | ClassNotFoundException e){
			e.printStackTrace();
		}finally {
			if(rs!=null){
				try {
					rs.close();
					rs = null;
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (st!=null){
				try {
					st.close();
					st = null;
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(ct!=null)
			try {
				ct.close();
				ct = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return alist;
	}

}
